package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.utils.CurrentHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    //根据分类类型type查询分类
    @Override
    public List<Category> list(Integer type) {

       List<Category>  category = categoryMapper.list(type);
        return category;
    }

    //分页查询
    @Override
    public PageResult pageQuery(int page, int pageSize, String name, Integer type) {
        PageHelper.startPage(page, pageSize);
        Page<Category> list =(Page) categoryMapper.page(name,type);
        return new PageResult(list.getTotal(),list.getResult());
    }

    //新增 分类
    @Override
    public void save(CategoryDTO categoryDTO) {
        //新增基础信息
        Category category = new Category();
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(CurrentHolder.getCurrentId());
        category.setUpdateUser(CurrentHolder.getCurrentId());
        category.setStatus(1);
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.save(category);
    }

    //删除 分类
    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    //修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(CurrentHolder.getCurrentId());
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.update(category);
    }

//    启用禁用
    @Override
    public void startOrStop(Integer status, Long id) {
        categoryMapper.setStatus(status,id);
    }
}
